﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    //****List of sensors, binds to datagridview to create list of current sensors
    class SensorDisplay : IObserver
    {
        public BindingList <SensorData> sensorlist =new BindingList<SensorData>();
        public SensorDisplay(List<Sensor> sensors)
        {            
            foreach (Sensor s in sensors)//create new sensordata instance
            {
                //Create new SensorData instance from Sensor s so that it may be added to the BindingList
                //(cannot add normal Sensor type to BindingList
                sensorlist.Add(new SensorData(s.getName(),dataToString(s.getData())));
            }
        }
        public void Update(string name, object msg)
        {
            int index=0;//find index of the Sensor to be removed
            foreach (SensorData s in sensorlist)
            {
                if (name.Equals(s.Name))//find the sensor being updated
                    //in the list
                {
                    s.Value = dataToString(msg);
                    sensorlist.ResetItem(index);
                    break;
                }
                index++;
            }
        }
        private string dataToString(object msg)//Converts data sent by sensor to usable string
        {//Perhaps won't even use in the future
            if (msg == null)
                return "Sensor Inactive";
            Type t=msg.GetType();
            if (t==typeof(System.String))
            {
                return (string)msg;
            }
            else
            {
                if (t.ToString().EndsWith("[]"))//*******Needs better test
                {
                    if (msg.GetType() == typeof(System.Diagnostics.Process[]))//if sent from ProcessSensor
                    {
                        string ret = "";
                        Process[] proc = (Process[])msg;
                        foreach (Process p in proc)
                        {
                            ret+=(p.ProcessName+", ");
                        }
                        return ret;
                    }
                }
                else if (msg.GetType().ToString().StartsWith("System.Collections.Generic.List"))
                    //*******Need better test for list type
                {
                    string ret="";
                    List<string> l = (List<string>)msg;
                    foreach (string s in l)
                    {
                        ret+=s+", ";
                    }
                    return ret;
                }
                else if (t == typeof(System.Single))
                {
                    return ((float)msg).ToString();
                }
            }
            return "Data could not be displayed";
        }
    }
}
